lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

Basic functionality.md (1728B)


      1 +++
      2 title = 'Basic functionality'
      3 +++
      4 # Basic functionality
      5 ## Variables
      6 ```cpp
      7 int inch;
      8 bool answer=true;
      9 ```
     10 
     11 ## Fundamental types
     12 - integral: boolean (bool), character (char), integer (int)
     13 - arithmetic: floating-point (double), integral types
     14 - user-defined types: enumeration (enum) and classes
     15 - absence of information (void)
     16 
     17 from these, you can construct:
     18 - pointer types (int*, char*,…)
     19 - arrays (int[], char[], …)
     20 - reference (double&), data structures
     21 
     22 ## Operators
     23 - arithmetic: +, -, /, *, %
     24 - comparison: ==, !=, <, >, <=, >=
     25 - ‘put to’: <<
     26 - ‘get from’: >> — type accepted depends on the right-hand operand
     27 
     28 ## Tests (conditionals)
     29 ```cpp
     30 // simple function that asks user to proceed
     31 bool accept() {
     32     cout << “Do you want to proceed (yN)?\n”;
     33     char answer=0;
     34     cin >> answer;
     35 
     36     if (answer == ‘y’) return true;
     37     return false;
     38 }
     39 
     40 // more complex function, with a switch statement
     41 bool accept2() {
     42     cout << “Do you want to proceed (yN)?\n”;
     43     char answer=0;
     44     cin >> answer;
     45     switch (answer) {
     46     case ‘y’:
     47         return true;
     48     case ’n’:
     49         return false;
     50     default:
     51         cout << “Whatever, it’s a no.\n”;
     52         return false;
     53     }
     54 }
     55 ```
     56 
     57 ## Loops
     58 ```cpp
     59 while (condition) {
     60     // execute code
     61     // opt. increment iterator
     62 }
     63 
     64 for (int i=0; i<10; i++) {
     65     // execute code
     66 }
     67 ```
     68 
     69 ## Pointers & arrays
     70 - array declaration:
     71 	```cpp
     72 	char v[10];    // array of 10 chars, with 0 as lower bound
     73 	```
     74 - pointer declaration:
     75 	```cpp
     76 	char* p;    // pointer to a char element
     77 	```
     78 - setting a pointer:
     79 	```cpp
     80 	p = &v[3];    // p points to address of index 3 of array v
     81 	```